package com.itheima.day06_demo;

/**
 * 某系统的数字密码是一个四位数，如1983，为了安全，需要加密后再传输，加密规则是：对密码中的每位数，
 * 都加5，再对10求余，最后将所有数字顺序反转，得到一串加密后的新数，请设计出满足本需求的加密程序！
 */
public class Demo4_Digital_encryption {
    public static void main(String[] args) {
        //目标：完成数字加密程序的开发
        System.out.println("getEncryptionCode(1982) = " + getEncryptionCode(1982));
        System.out.println("getEncryptionCode(1982) = " + getEncryptionCode(7346));
    }
    public static String getEncryptionCode(int number){
        //number = 1982;
        //1.把这个密码拆分成一个一个的数字，才可以对其进行加密
        int[] numbers = split(number);
        //2.遍历数组中的每个数字，对其进行加密处理
        for (int i = 0; i < numbers.length; i++) {
            numbers[i] =( numbers[i] + 5) % 10;

        }
        //3.对数组反转，把对数组进行反转的操作交给独立方法来完成
        reverse(numbers);

        //4.把数字拼接成字符串，返回
        String data = "";
        for (int i = 0; i < numbers.length; i++) {
            data += numbers[i];
        }
        return data;
    }
    public static void reverse(int[] numbers){
        int start = 0;
        int end = numbers.length - 1;
        while (start < end) {
            int temp = numbers[start];
            numbers[start] = numbers[end];
            numbers[end] = temp;
            start++;
            end--;
        }
    }
    public static int[] split(int number){
        int[] numbers = new int[4];
        numbers[0] = number / 1000;
        numbers[1] = number / 100 % 10;
        numbers[2] = number / 10 % 10;
        numbers[3] = number % 10;
        return numbers;
    }

}
